package com.dao;

import com.utils.DBUtils;
import com.dto.PageDTO;
import com.model.Employee;

import java.sql.*;
import java.util.ArrayList;


public class EmployeeDao {
public int selectCount(){
    try {
        Connection connection = DBUtils.getConnection();
        String sql = "select count(*) totalCount from t_employee";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            return resultSet.getInt("totalCount");
        }
        return 0;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

    public PageDTO<Employee> selectList(int pageNumber , int pageLimit){

    int totalCount = selectCount();
    int totalPage = totalCount / pageLimit;
    if (totalCount % pageLimit >0){
        totalPage++;
    }

        try {
            Connection connection = DBUtils.getConnection();
            int offset = (pageNumber - 1) *pageLimit;
            String sql = "select * from t_employee limit ?,?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setInt(1,offset);
            preparedStatement.setInt(2,pageLimit);

            ResultSet resultSet = preparedStatement.executeQuery();

            ArrayList<Employee> employees = new ArrayList<>();
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String empName = resultSet.getString("emp_name");
                String empGender = resultSet.getString("emp_gender");
                String empDept = resultSet.getString("emp_dept");
                double empSalary = resultSet.getDouble("emp_salary");

                Employee employee = new Employee(id, empName, empGender, empDept, empSalary);

                employees.add(employee);
            }
            PageDTO<Employee> pageDTO = new PageDTO<>(pageNumber, pageLimit, totalPage, totalCount, employees);
            return pageDTO;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public int delete( Integer id){
        return DBUtils.executeUpdate("delete from t_employee where id = ?",id);
    }



    public int insert(Employee employee){
        return DBUtils.executeUpdate("insert into t_employee(emp_name,emp_gender,emp_dept,emp_salary)values(?,?,1,?)",
                employee.getEmpName(), employee.getEmpGender(), employee.getEmpSalary());

    }

    public Employee selectById(int selectId){
        try {
            Connection connection = DBUtils.getConnection();
            String sql = "select * from t_employee where id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,selectId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                int id = resultSet.getInt("id");
                String empName = resultSet.getString("emp_name");
                String empGender = resultSet.getString("emp_gender");
                String empDept = resultSet.getString("emp_dept");
                double empSalary = resultSet.getDouble("emp_salary");
                Employee employee = new Employee(id, empName, empGender, empDept, empSalary);
                return employee;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int updateById(Employee employee){
        return DBUtils.executeUpdate("update t_employee set emp_name=?,emp_gender=?," +
                "emp_dept=?,emp_salary=? where id=?",employee.getEmpName(),employee.getEmpGender(),
                employee.getEmpDept(),employee.getEmpSalary(),employee.getId());
    }

}
